package com.joysuch.wwyt.mh.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.joysuch.wwyt.core.common.ajax.Result;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.enums.Status;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import com.joysuch.wwyt.mh.bean.CustomizeMonitorPointDataBean;
import com.joysuch.wwyt.mh.bean.CustomizeMonitorPointSearchBean;
import com.joysuch.wwyt.mh.bean.MhMonitoringPointReqBean;
import com.joysuch.wwyt.mh.dto.CustomizeMonitorTypeSelectDto;
import com.joysuch.wwyt.mh.bean.vo.CustomizeMonitorTypeVO;
import com.joysuch.wwyt.mh.dto.CustomizeMonitorLocationInfoDto;
import com.joysuch.wwyt.mh.dto.CustomizeMonitorPointDto;
import com.joysuch.wwyt.mh.entity.CustomizeMonitorPoint;
import com.joysuch.wwyt.mh.entity.CustomizeMonitorType;
import com.joysuch.wwyt.mh.service.CustomizeMonitorPointService;
import com.joysuch.wwyt.mh.service.CustomizeMonitorTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jingwk
 * @since 2021-07-05
 */
@RestController
@Api(tags = "自定义监测点管理")
@RequestMapping("/mh/customize-monitoring")
public class CustomizeMonitorPointController {

    @Autowired
    private CustomizeMonitorPointService customizeMonitoringPointService;
    @Autowired
    private CustomizeMonitorTypeService customizeMonitoringTypeService;

    @PostMapping("/point/saveOrUpdate")
    @ApiOperation(value = "监测点保存或修改")
    @RequiresPermissions("mh:customize-monitoring-point:save")
    public ResultBean saveOrUpdate(@RequestBody CustomizeMonitorPoint customizeMonitoringPoint) {
        customizeMonitoringPointService.saveOrUpdate(customizeMonitoringPoint);
        return ResultBean.defaultSuccessResult();
    }


    @PostMapping("/batchSwitch")
    @ApiOperation(value = "自定义监测点批量开关")
    public ResultBean batchSwitch(@RequestBody MhMonitoringPointReqBean bean){
     customizeMonitoringPointService.batchSwitch(bean);
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("/point/delete")
    @ApiOperation(value = "删除监测点")
    @RequiresPermissions("mh:customize-monitoring-point:delete")
    public ResultBean delete(Long pointId) {
        CustomizeMonitorPoint point = new CustomizeMonitorPoint();
        point.setDeleteFlag("1");
        point.setId(pointId);
        customizeMonitoringPointService.updateById(point);
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("/point/list")
    @ApiOperation(value = "监测点列表")
    public ResultBean list(CustomizeMonitorPointSearchBean searchBean) {
        IPage<CustomizeMonitorPointDto> list = customizeMonitoringPointService.findListPaging(searchBean);
        return ResultBean.pageData(list.getRecords(), list.getTotal());
    }

    @GetMapping("/point")
    @ApiOperation(value = "监测点详情")
    public ResultBean list(Long pointId) {
        CustomizeMonitorType type = customizeMonitoringPointService.getPointTypeInfoById(pointId);
        return ResultBean.success(type);
    }

    @PostMapping("/type/saveOrUpdate")
    @ApiOperation(value = "监测点类型保存或修改")
    @RequiresPermissions("mh:customize-monitoring-type:save")
    public ResultBean saveOrUpdate(@RequestBody CustomizeMonitorType customizeMonitoringType) {
        try {
            customizeMonitoringTypeService.saveMonitorType(customizeMonitoringType);
            return ResultBean.defaultSuccessResult();
        } catch (ApiBusinessException e) {
            return ResultBean.fail(e.getCode(), e.getMessage());
        }
    }

    @PostMapping("/type/delete")
    @ApiOperation(value = "删除监测点类型")
    @RequiresPermissions("mh:customize-monitoring-type:delete")
    public ResultBean typeDelete(Long typeId) {
        customizeMonitoringTypeService.deleteById(typeId);
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("/type/list")
    @ApiOperation(value = "监测点类型列表")
    public ResultBean list(int page, int size) {
        IPage<CustomizeMonitorTypeVO> list = customizeMonitoringTypeService.findTypeInfoListPaging(page, size);
        return ResultBean.pageData(list.getRecords(), list.getTotal());
    }

    @GetMapping("/type/all")
    @ApiOperation(value = "全部监测点类型列表")
    public ResultBean allType() {
        List<CustomizeMonitorTypeSelectDto> list = customizeMonitoringTypeService.allType();
        return ResultBean.success(list);
    }

    @PostMapping("/point/push")
    @ApiOperation(value = "监测点推送")
    public Result pushMonitorPoint(@RequestBody CustomizeMonitorPointDataBean data) {
        try {
            customizeMonitoringPointService.pushMonitorPoint(data);
            return new Result(Status.SUCCESS.getCode(), "");
        } catch (ApiBusinessException e) {
            return new Result(e.getCode(), e.getMessage());
        }
    }

    @PostMapping("/point/push-to-socket")
    @ApiOperation(value = "监测点数据推送到socket")
    public ResultBean pushMonitorPoint() {
        customizeMonitoringPointService.pushMonitorPoint();
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("/point/location")
    @ApiOperation(value = "监测点大屏标志信息")
    public ResultBean locationInfo() {
        List<CustomizeMonitorLocationInfoDto> list = customizeMonitoringPointService.getLocationInfo();
        return ResultBean.success(list);
    }

    @GetMapping("/point/detail")
    @ApiOperation(value = "监测点详细信息")
    public ResultBean detail(Long pointId) {
        try {
            Object obj = customizeMonitoringPointService.monitorPointDetail(pointId);
            return ResultBean.success(obj);
        } catch (ApiBusinessException e) {
            return ResultBean.fail(e.getCode(), e.getMessage());
        }
    }
}

